<template>
    <div class="content">
        <div class="flex-start">
            <div class="shuxing"></div>
            <div class="page-title">基本信息</div>
        </div>
        <div class="flex-start m-t-24">
            <div class="label">
                <span>公司标志:</span>
            </div>
            <div>
                <el-upload :show-file-list="false" accept=".png, .jpg, .jpeg" class="upload-demo"
                    :http-request="customUpload" :limit="1" :on-success="handleLogoSuccess" :on-error="handleLogoError"
                    :file-list="fileList">
                    <img class="companylogo" v-if="form.company_logo == ''"
                        src="@/static/engineering5188/supplier/upload.png" alt="" />
                    <img class="companylogo" v-else :src="form.company_logo" alt="" />
                </el-upload>
            </div>

            <div class="justify-between">
                <div class="m-l-27 ">
                    <div class="name"> 会员等级：VIP会员</div>
                    <div class="renewval">立即续费</div>
                </div>

                <div class="name">会员期限：2024-04-30至2025-04-30（剩余<span class="red">256</span>天）</div>
            </div>
        </div>
        <div class="flex-start m-t-17">
            <div class="label">
                <span class="red">*</span>
                <span>公司名称:</span>
            </div>
            <input type="text" v-model="form.company_name"></input>
        </div>
        <div class="justify-between m-t-17">
            <div class="flex-start">
                <div class="label">
                    <span>地址:</span>
                </div>
                <el-cascader class="cascaderClass"  v-model="address" :options="treeData" style="flex-grow: 0;flex-shrink: 0;"></el-cascader>
            </div>
            <div class="flex-start">
                <div class="name">成立时间：</div>
                <el-date-picker align="right" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="form.register_time"
                    class="cascaderClass">
                </el-date-picker>
            </div>
        </div>
        <div class="justify-between m-t-17">
            <div class="flex-start">
                <div class="label">
                    <span>注册资本:</span>
                </div>
                <input type="text" class="cascaderClass" v-model="form.registered_capital"></input>
                <!-- <el-select class="cascaderClass" style="flex-grow: 0;flex-shrink: 0;" v-model="form.registered_capital"
                    placeholder="请选择">
                    <el-option v-for="item in optionsMoney" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select> -->
            </div>
            <div class="flex-start">
                <div class="name">主要品牌：</div>
                <input type="text" class="cascaderClass" v-model="form.company_brands"></input>
            </div>
        </div>
        <div class="flex-start m-t-17">
            <div class="label">
                <span>主营类别:</span>
            </div>
            <el-select style="width:100%" v-model="form.company_cats" placeholder="请选择">
                <el-option v-for="item in options" :key="item.id" :label="item.title" :value="item.id">
                </el-option>
            </el-select>
        </div>
        <div class="flex-start m-t-17">
            <div class="label">
                <span class="red">*</span>
                <span>主营产品:</span>
            </div>
            <input type="text" v-model="form.company_products"></input>
        </div>
        <div class="flex-start m-t-17" style="align-items: flex-start;">
            <div class="label">
                <span>公司简介：</span>
            </div>
            <textarea id="myTextarea" v-model="form.company_introduction"></textarea>
        </div>


        <div class="flex-start  m-t-24">
            <div class="shuxing"></div>
            <div class="page-title">实名认证</div>
        </div>
        <div class="flex-start m-t-24">
            <div class="label">
                <span>营业执照附件:</span>
            </div>
            <div>
                <el-upload class="upload-demo" drag :http-request="customUpload" :limit="1"
                    :on-success="handleLicenseSuccess" :on-error="handleLicenseError" accept=".png, .jpg, .jpeg,.pdf"
                    :show-file-list="false" :file-list="fileList">
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                </el-upload>
            </div>
            <div class="uploadRight m-l-14">
                <div class="flex-space-between" style="width:100%" v-if="form.business_license.name">
                    <div class="flex-start">
                        <img class="fileIcon" src="@/static/engineering5188/supplier/file.png" alt="">
                        <span @click="openTab" style="cursor: pointer;">{{ form.business_license.name }}</span>
                    </div>
                    <img class="gouxuanIcon" src="@/static/engineering5188/supplier/gouxuan.png" alt="">
                </div>
                <div class="tips">文件大小不超过10MB；格式支持：jpg、png、jpeg、pdf</div>
            </div>
        </div>
        <div class="flex-start m-t-24">
            <div class="label">
                <span> </span>
            </div>
            <div class="flex-start">
                <div class="btnCancel">取消</div>
                <div class="btnSave" @click="saveData">保存</div>
            </div>
        </div>
    </div>
</template>

<script>
import { mapState } from "vuex";
import axios from "axios";
import { values } from "lodash";
const { _sheng, _shi, _qu } = require('../company/city.data');

export default {
    computed: {
        ...mapState([
            "userInfo",
            'token'
        ]),
    },
    data() {
        return {
            fileList: [],
            options: [],
            form: {
                company_logo: '',
                company_name: "",
                register_time: '',
                company_brands: '',
                company_products: '',
                company_introduction: "",
                registered_capital: '',
                company_cats: '',
                business_license: {},
            },
            gongshang_info:{
                no:'',
                type:'',
                representative:'',
                business_term:'',
                status:'',
                authority:'',
                approval:'',
                address:'',
                business_scope:'',
            },
            treeData: [],
            address:[],
            optionsMoney:[]
        }
    },
    watch: {
        userInfo: {
            handler(val) {
                if (val.business_license == '') {
                    val.business_license = {};
                } else {
                    val.business_license = JSON.parse(val.business_license);
                }
                this.form = val;
            },
            deep: true
        }
    },
    created() {
        console.log('dedee', this.userInfo)
        this.form = this.userInfo;
        this.address = [this.form.province,this.form.city,this.form.district];
        this.treeData = this.buildTree(_sheng, _shi, _qu);
        this.getOptions();
    },
    methods: {
        openTab(){
            window.open(this.form.business_license.url,'_blank');
        },
        getOptions(){
            this.$httpApi("/api/getProductCat",{
                parent_id:0
            },'get').then(res =>{
                res.data.forEach(element => {
                    element.id = element.id+'';
                });
                this.options = res.data;
                console.log(res)
            })
        },
        buildTree(sheng, shi, qu) {
            let tree = sheng.map(province => {
                let cities = shi.filter(city => city.sheng_code === province.sheng_code).map(city => {
                    let districts = qu.filter(district => district.shi_code === city.shi_code).map(district => {
                        return {
                            ...district,
                            value:district.qu_name,
                            label: district.qu_name
                        };
                    });
                    return {
                        ...city,
                        value:city.shi_name,
                        label: city.shi_name,
                        children: districts
                    };
                });
                return {
                    ...province,
                    value: province.sheng_name,
                    label: province.sheng_name,
                    children: cities
                };
            });
            return tree;
        },
        handleChange(file, fileList) {
            console.log(file, fileList)
            this.fileList = fileList;
        },
        saveData() {
            console.log(this.address,'address');
            this.$httpApi("/api/updateUser", {
                company_logo: this.form.company_logo,
                company_name: this.form.company_name,
                register_time: this.form.register_time,
                company_brands: this.form.company_brands,
                company_products: this.form.company_products,
                company_introduction: this.form.company_introduction,
                registered_capital: this.form.registered_capital,
                company_cats: this.form.company_cats,
                business_license: JSON.stringify(this.form.business_license),
                province: this.address.length >0 ?this.address[0]:'',
                city: this.address.length >0 ?this.address[1]:'',
                district: this.address.length >0 ?this.address[2]:'',
                token: this.token,
            }, "post").then(res => {
                if (res.code == 200) {
                    this.$store.dispatch("getUserloginedInfo");
                    alertSucc('保存成功');
                } else {
                    alertErr(res.msg);
                }
            })
        },
        async customUpload({ file, onProgress, onSuccess, onError }) {
            const formData = new FormData();
            formData.append("file", file);
            formData.append("token", this.token);
            debugger
            try {
                const response = await axios.post("/api/upload", formData, {
                    headers: { "Content-Type": "multipart/form-data" },
                    onUploadProgress: (progressEvent) => {
                        if (progressEvent.lengthComputable) {
                            const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total);
                            onProgress({ percent: progress }); // 更新进度条
                        }
                    },
                });
                console.log(response, 'edededede')
                if (response.code == 200) {
                    onSuccess(response.data); // 通知组件上传成功
                } else {
                    alertErr(response.msg)
                }

            } catch (error) {
                // onError(error); // 通知组件上传失败
                alertErr('上传失败');
            }
        },
        handleLogoSuccess(response, file, fileList) {
            if (response) {
                this.form.company_logo = response.url;
            }
        },
        handleLogoError(error, file, fileList) {
            console.log(error, file, fileList)
        },
        handleLicenseSuccess(response, file, fileList) {
            if (response) {
                this.form.business_license = {
                    url: response.url,
                    name: response.name
                };
            }
            console.log('eeeeeeeeeeeeeee', response, file, fileList)
        },
        handleLicenseError(error, file, fileList) {
            console.log(error, file, fileList)
        },

    }
}
</script>

<style scoped lang="less">
.content {
    padding-top: 32px;
    padding-left: 32px;
    padding-bottom: 26px;
    padding-right: 94px;
    background: #FFFFFF;

    .shuxing {
        width: 6px;
        height: 18px;
        background: #31A41B;
        border-radius: 3px 3px 3px 3px;
    }

    .flex-start {
        display: flex;
        align-items: center;
    }

    .page-title {
        font-family: PingFang SC, PingFang SC;
        font-weight: 500;
        font-size: 16px;
        color: #333333;
        margin-left: 10px;
    }

    .m-t-24 {
        margin-top: 24px;
    }

    .label {
        flex-basis: 144px;
        flex-grow: 0;
        /* 禁止放大 */
        flex-shrink: 0;
        /* 禁止缩小 */
        text-align: right;
        font-weight: 400;
        font-size: 14px;
        color: #333333;
        margin-right: 10px;
    }

    .companylogo {
        width: 120px;
        height: 74px;
    }

    .renewval {
        width: 100px;
        height: 36px;
        background: #EAF6E8;
        border: 1px solid #31A41B;
        font-weight: bold;
        font-size: 14px;
        color: #31A41B;
        text-align: center;
        line-height: 36px;
        border-radius: 20px;
        margin-top: 11px;
    }

    .name {
        text-align: right;
        font-weight: 400;
        font-size: 14px;
        color: #333333;
    }

    .red {
        color: #E5020D;
    }

    .m-l-27 {
        margin-left: 27px;
    }

    .m-l-14 {
        margin-left: 14px;
    }

    .justify-between {
        display: flex;
        justify-content: space-between;
        flex-grow: 1;
    }

    .m-t-17 {
        margin-top: 17px;
    }

    .cascaderClass {
        width: 260px;
        flex-basis: 260px;
        flex-grow: 0;
        /* 禁止放大 */
        flex-shrink: 0;

        .el-input__inner {
            background: #FAFAFA;
        }
    }

    .btnCancel {
        width: 133px;
        height: 40px;
        background: #F5F5F5;
        border-radius: 4px 4px 4px 4px;
        border: 1px solid #D7D7D7;
        font-weight: 400;
        font-size: 14px;
        color: #666666;
        text-align: center;
        line-height: 40px;
        cursor: pointer;
    }

    .btnSave {
        width: 133px;
        height: 40px;
        background: #31A41B;
        border-radius: 4px 4px 4px 4px;
        border: 1px solid rgba(0, 0, 0, 0);
        font-weight: 400;
        font-size: 14px;
        color: #FFFFFF;
        cursor: pointer;
        text-align: center;
        line-height: 40px;
        margin-left: 12px;
    }

    .tips {
        font-weight: 400;
        font-size: 12px;
        color: #999999;
        line-height: 24px;
    }

    .fileIcon {
        width: 20px;
        height: 20px;
        margin-right: 6px;
    }

    .gouxuanIcon {
        width: 12px;
        height: 12px;
    }

    .flex-space-between {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .uploadRight {
        display: flex;
        height: 120px;
        align-items: flex-end;
        flex-direction: column;
        justify-content: flex-end;
    }
}

/deep/.el-upload-dragger {
    background: #FAFAFA;
    width: 200px;
    height: 120px;

    .el-icon-upload {
        font-size: 48px;
        margin: 0px;
        margin-top: 16px;
        margin-bottom: 16px;
    }

    .el-upload__text {
        color: #A2A2A2;
    }

    em {
        color: #A2A2A2;
    }
}

/deep/.el-upload-dragger:hover {
    border: 1px solid #FAFAFA;
}

input {
    flex: 2;
    height: 100%;
    padding-left: 16px;
    font-size: 14px;
    color: #000;
    background: #FAFAFA;
    border-radius: 4px 4px 4px 4px;
    border: 1px solid #E6E5E5;
    height: 40px;

    &::-webkit-input-placeholder {
        font-size: 14px;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
        font-weight: 400;
        color: #d7d7d7;
    }
}

textarea {
    flex: 2;
    height: 100%;
    padding: 16px;
    font-size: 14px;
    color: #000;
    background: #FAFAFA;
    border-radius: 4px 4px 4px 4px;
    border: 1px solid #E6E5E5;
    height: 160px;

    &::-webkit-input-placeholder {
        font-size: 14px;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
        font-weight: 400;
        color: #d7d7d7;
    }
}

textarea:focus {
    outline: none;
    border: 1px solid #E6E5E5;
}

/deep/.el-input__inner {
    background: #FAFAFA;
}
</style>